#include<iostream>
#include<cstring>
#include<map>
using namespace std;
int cc;
int n;
string s1,s2,s12;
int flag,ans;
map<string,int> mp;
void dfs(string a,string b,string c){
	ans++;
	string res = "";
	for(int i = 0;i < cc;i++){
		res += b[i];
		res += a[i];
	}
	mp[res]++;
	//重复出现
	if(mp[res] > 1){
		flag = 1;
		return ;
	}
	//找到结果
	if(res == c){
		return ;
	}
	//继续模拟
	for(int i = 0;i < cc;i++){
		a[i] = res[i];
		b[i] = res[cc + i];
	}
	dfs(a,b,c);
}
int main(){
	cin >> n;
	int t = 0;
	while(n--){
		t++;
		flag = 0;
		cin >> cc;
		cin >> s1 >> s2;
		cin >> s12;
		mp.clear();
		ans = 0;
		dfs(s1,s2,s12);
		if(flag) ans = -1;
		cout << t << " " << ans << "\n";
	}
}